#include "Model.h"

Model::Model():m_pModel(0)
{
	m_pModel = new ModelOBJ();
}

Model::~Model()
{
	delete m_pModel;
}

void Model::Load(char* filename)
{
	if (m_pModel->import(filename))
    {
        m_pModel->normalize();

		/*
        for (int i = 0; i < m_pModel->getNumberOfMaterials(); ++i)
        {
            const ModelOBJ::Material &material = m_pModel->getMaterial(i);

            if (material.colorMapFilename.empty())
                continue;

            std::string filename = "Content/Textures/" + material.colorMapFilename;
            GLuint textureId = LoadTexture(filename.c_str());

            if (!textureId)
                throw std::runtime_error("Failed to load texture: \"" + filename + "\"");
            else
                g_modelTextures[material.colorMapFilename] = textureId;
        }
		*/
    }
    else
    {
        throw std::runtime_error("Failed to load model.");
    }
}

void Model::Draw()
{
	Quaternion          meshOrientation = Quaternion();
	Vector3             meshPosition = Vector3(0.0f,0.0f,0.0f);

	glPushMatrix();

    //Matrix4 m = meshOrientation.toMatrix4();

    //m[3][0] = meshPosition.x;
    //m[3][1] = meshPosition.y;
    //m[3][2] = meshPosition.z;

   /* glMultMatrixf(&m[0][0]);*/

    GLuint textureId = 0;
    const ModelOBJ::Mesh *pMesh = 0;
    const ModelOBJ::Material *pMaterial = 0;
    const ModelOBJ::Vertex *pVertices = 0;

    for (int i = 0; i < m_pModel->getNumberOfMeshes(); ++i)
    {
        pMesh = &m_pModel->getMesh(i);
        pMaterial = &m_pModel->getMaterial(pMesh->materialIndex);
        pVertices = m_pModel->getVertexBuffer();

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, pMaterial->ambient);
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pMaterial->diffuse);
        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pMaterial->specular);
        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, pMaterial->shininess * 128.0f);

		/*
        if ((textureId = g_modelTextures[pMaterial->colorMapFilename]) != 0)
        {
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, textureId);
        }
        else
        {
            glDisable(GL_TEXTURE_2D);
        }
		*/

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, m_pModel->getVertexSize(), pVertices->position);

        if (m_pModel->hasTextureCoords())
        {
            glActiveTextureARB(GL_TEXTURE0_ARB);
            glEnable(GL_TEXTURE_2D);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glTexCoordPointer(2, GL_FLOAT, m_pModel->getVertexSize(), pVertices->texCoord);
        }

        if (m_pModel->hasVertexNormals())
        {
            glEnableClientState(GL_NORMAL_ARRAY);
            glNormalPointer(GL_FLOAT, m_pModel->getVertexSize(), pVertices->normal);
        }

        glDrawElements(GL_TRIANGLES, pMesh->triangleCount * 3,
            GL_UNSIGNED_INT, m_pModel->getIndexBuffer() + pMesh->startIndex);

        if (m_pModel->hasVertexNormals())
            glDisableClientState(GL_NORMAL_ARRAY);

        if (m_pModel->hasTextureCoords())
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        glDisableClientState(GL_VERTEX_ARRAY);
    }

    glPopMatrix();
}

